package com.thx.information.dao;

import com.thx.common.orm.PaginationDao;
import com.thx.information.model.InfoCommentLog;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class CommentLogDao extends PaginationDao<InfoCommentLog> {

  private static final Logger logger = LoggerFactory.getLogger(CommentLogDao.class);

  /**
   * 根据新闻id分页查询所有评论，并按评论时间倒排序.
   * 
   * @param infoId 新闻id,不能为空
   * @return 分页新闻评论信息列表
   */
  public List<InfoCommentLog> findByInfoId(Long infoId) {
    String hql = "from InfoCommentLog where infoId=? order by commentDate desc";
    return this.findPageList(hql, new Object[] {infoId});
  }

  /**
   * 根据新闻id查询所有评论，并按评论时间倒排序.
   * 
   * @param infoId 新闻id,不能为空
   * @return 新闻评论信息列表
   */
  public List<InfoCommentLog> findAllByInfoId(Long infoId) {
    String hql = "from InfoCommentLog where infoId=? order by commentDate desc";
    return createQuery(hql, new Object[] {infoId}).list();
  }

  public List<InfoCommentLog> queryAll(Long enterpriseId, Long infoId) {
    StringBuffer hql = new StringBuffer();
    hql.append("from InfoCommentLog");
    List<Object> objs = new ArrayList<Object>();
    this.getHql(hql, objs, enterpriseId, infoId);
    return this.findPageList(hql.toString(), objs.toArray());
  }

  public int queryCount(Long enterpriseId, Long infoId) {
    StringBuffer hql = new StringBuffer();
    hql.append("select count(*) from InfoCommentLog");
    List<Object> objs = new ArrayList();
    this.getHql(hql, objs, enterpriseId, infoId);
    Object obj = this.createQuery(hql.toString(), objs.toArray()).uniqueResult();
    return obj != null ? Integer.parseInt(obj.toString()) : 0;
  }

  /**
   * 查询栏目列表sql语句 建立日期 : Apr 23, 2013 <br>
   * 作者 : 王阿敏<br>
   * 
   * @param info
   * @return
   */
  public void getHql(StringBuffer hql, List<Object> objs, Long enterpriseId, Long infoId) {
    if (enterpriseId != null && enterpriseId != 0L) {
      hql.append("enterpriseId = ?");
      objs.add(enterpriseId);
      if (infoId != null && infoId != 0L) {
        hql.append(" and infoId = ?");
        objs.add(infoId);
      }
      hql.append(" order by commentDate desc ");
    }
  }

  /**
   * 根据消息id查询该消息评论数量
   * @param infoId 新闻id,不能为空
   * @return 消息评论数量
   */
  public int queryCountByInfoId(Long infoId) {
    int result = 0;
    if (infoId != null && infoId != 0L) {
      StringBuffer hql = new StringBuffer();
      hql.append("select count(*) from InfoCommentLog where infoId = ?");
      List<Object> objs = new ArrayList();
      objs.add(infoId);
      Object obj = this.createQuery(hql.toString(), objs.toArray()).uniqueResult();
      result = obj != null ? Integer.parseInt(obj.toString()) : 0;
    }
    return result;
  }
}
